<template>
  <div id="app">
    <!-- <router-view v-wechat-title="$route.meta.title"></router-view> -->

    <keep-alive>
      <router-view v-if="$route.meta.keepAlive" v-wechat-title="$route.meta.title"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive" v-wechat-title="$route.meta.title"></router-view>
    
    <!-- <div class="load_out"></div> -->
    <!-- <img class="load Rotation" src="./assets/loading.png"> -->
    <!-- <p style="position: fixed;z-index: 1500;color: red;top: 7rem;left: 0;height: 2rem;line-height: 2rem;background-color: #000;" @click="remove">{{myUuid}}</p> -->
    <!-- <p style="position: fixed;z-index: 1500;color: red;top: 14rem;left: 0;height: 2rem;line-height: 2rem;background-color: #000;" @click="remove">移除缓存</p> -->
    
  </div>
</template>

<script>
import global from './utils/global'
let Base64 = require('js-base64').Base64;
export default {
  name: 'App',
  provide () {
    return {
      reload: this.reload,
    }
  },
  data () {
    return {
      isRouterAlive: true,
      myUuid: '0',
    }
  },
  beforeCreate(){
  },
  created(){
    this.getParams();
  },
  beforeMount(){

  },
  mounted(){


  },
  updated(){
    // this.getParams();
  },
  beforeDestroy(){
  },
  activated: function () {
    // this.getParams();
  },
  watch: {

    $route(to,from){

      var that = this;

      if (to.name == 'home' && localStorage.getItem("carWashJudge") == 1) {

        that.getParams();

      }

    },

  },
  methods:{
    // 获取uuid
    getUrlStr(name){
      let reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)","i");
      // this.window = window.location.href;s
      this.window = window.location.href.split('#')[0];
      if(reg.test(window.location.href)){
        return unescape(RegExp.$2.replace(/\+/g," "))
      }
      return undefined
    },

    toLogin(locurl,parentUid){

      console.log("toLogin");
      console.log(parentUid);

      // alert("toLogin");
      // alert(locurl);
      // alert(parentUid);

      sessionStorage.setItem("locurl", locurl);

      if (typeof(parentUid) != undefined) {

        sessionStorage.setItem("parentUid", parentUid);

      } else {

        sessionStorage.setItem("parentUid", "0");

      }
      
      this.$router.push({
        path: '/login'
      })

    },

    getParams(){

      console.log("-----------------开始测试-----------------");
      // alert("开始测试");

      var that = this;

      var locurl = window.location.href;
      
      locurl = Base64.encode(locurl.split('?')[0]);

      // var parentUid = that.getUrlStr('parentUid').split('#')[0];
      // var paramsUuid = that.getUrlStr('uuid').split('#')[0];
      var parentUid = that.getUrlStr('parentUid');
      var paramsUuid = that.getUrlStr('uuid');

      var carWashUuid = localStorage.getItem("carWashUuid");
      var carWashJudge = localStorage.getItem("carWashJudge");

      console.log(parentUid);
      console.log(paramsUuid);
      console.log(carWashUuid);
      console.log(carWashJudge);
      
      // alert(parentUid);
      // alert(paramsUuid);
      // alert(carWashUuid);
      // alert(carWashJudge);

      if (!carWashUuid && typeof(carWashUuid)!='undefined' && carWashUuid!=0) {  //localStorage  中没有  uuid

        if (typeof(paramsUuid) != undefined) {  //url  中含有参数

          if (carWashJudge == 1) {

            // alert("位置1");
            console.log("位置1");
            that.myUuid = paramsUuid.split('#')[0];
            
            localStorage.setItem("carWashUuid", paramsUuid.split('#')[0]);
            localStorage.setItem("carWashJudge", 0);
            
            console.log(localStorage.getItem("carWashUuid"));
            // alert(localStorage.getItem("carWashUuid"));

          } else if (!carWashUuid && typeof(carWashUuid)!='undefined' && carWashUuid!=0) {

            // console.log("from 1");
            // alert("位置2");
            console.log("位置2");
            that.toLogin(locurl,parentUid);

          }

        } else {

          // alert("位置3");
          console.log("位置3");
          that.toLogin(locurl,parentUid);

        }

      } else if (carWashJudge == 1) {

        // alert("位置4");
        console.log("位置4");

        localStorage.setItem("carWashUuid", paramsUuid.split('#')[0]);
        localStorage.setItem("carWashJudge", 0);
        that.myUuid = paramsUuid.split('#')[0];

        window.location.href = window.location.href.split('?')[0];

      } else if (carWashUuid == 0) {

    
        console.log("位置5");
        localStorage.setItem("carWashUuid", paramsUuid.split('#')[0])


      } else {

        // alert("位置6");
        console.log("位置6");

      }

    },

    getUuid(){
      var that = this;

      var url = window.location.href;
      var pathUrl = Base64.encode(url);

      choose ();

      function choose () {
        if (that.getUrlStr('uuid') != undefined) {
          localStorage.setItem('carWashUuid', 
          that.getUrlStr('uuid'));
          localStorage.getItem('carWashUuid');
        }

        if (localStorage.getItem('carWashUuid') != null) {
          if (that.getUrlStr('uuid') != undefined) {
            var toUrl = url.split('?')[0];
            window.location.href = '/#/login';
          }
        } else {
          window.location.href = global.api.baseurl+"app/index.php?i=2&c=entryApi&do=user&m=yimi_car&op=weqingLogin&url=" + pathUrl;
          // window.location.href = "/app/index.php?i=2&c=entryApi&do=user&m=yimi_car&op=weqingLogin&url=" + pathUrl;
          setTimeout(()=>{
            // choose ();
            console.log("over");
          },1000)
        }
      }

    },
    // 重新加载
    reload(){
      this.isRouterAlive = false;
      this.$nextTick(function(){
        this.isRouterAlive = true
      })
    },
    test(){
      alert(window.location.href);
    },
    remove(){
      alert("remove");
      localStorage.removeItem('carWashUuid');
      localStorage.removeItem('carWashJudge');
    },
  },
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  /* margin-top: 60px; */
  background-color: #fff;
  position: relative;
}
.load_out{
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1099;
  width: 100%;
  height: 100%;
  background-color: rgba(0,0,0,0.3);
}
/*.load{
  position: fixed;
  top: 50%;
  left: 50%;
  z-index: 1100;
  width: 5rem;
  height: 5rem;
  margin-left: -2.5rem;
  margin-top: -5rem;
}
.load_text{
  position: fixed;
  top: 50%;
  left: 0;
  z-index: 1100;
  width: 100%;
  height: 2rem;
  line-height: 2rem;
}
.hide{
  display: none;
}
@-webkit-keyframes rotation{
  from {-webkit-transform: rotate(0deg);}
  to {-webkit-transform: rotate(360deg);}
}
.Rotation{
  -webkit-transform: rotate(360deg);
  animation: rotation 1.5s linear infinite;
  -moz-animation: rotation 1.5s linear infinite;
  -webkit-animation: rotation 1.5s linear infinite;
  -o-animation: rotation 1.5s linear infinite;
}*/
*{
  margin: 0;
  padding: 0;
}
html,body{
  margin: 0;
  padding: 0;
}
</style>
